<template>
  <div class="container">
    <div class="lang-div">
      <button @click.stop="changeLang">切换语言</button>
      <span>当前语言： {{ lang }}</span>
    </div>
    <plan ref="planRef" v-model="planOption" />
    <policyholder ref="policyholderRef" v-model="policyholderOption" />
    <insured ref="insuredRef" v-model="insureOption" />

    <div class="bottom_fixed">
      <van-button block class="mt10" type="primary" @click.stop="saveEvt">
        <span class="money" v-if="totalPremium">（￥{{ totalPremium }}）</span>
        保存
      </van-button>
    </div>
  </div>

</template>

<script lang="ts" setup>
import { ref } from 'vue'
import { planOption, policyholderOption, insureOption } from './config.js'; // 导入共享状态
import customerInfoClass from './indexClass.js'; // 导入单例实例
import i18n, { setLang } from '@/lang/m/index' // i18n
import * as apis from '@/apis/index'
const lang = ref(i18n.global.locale)

// 切换语言
const changeLang = () => {
  setLang(lang.value == 'zh-cn' ? 'en-us' : 'zh-cn')
  location.reload()
}

const totalPremium = ref()

const planRef = ref()
const policyholderRef = ref()
const insuredRef = ref()


// 将投保数据传递给单例实例
customerInfoClass.planOption = planOption;
customerInfoClass.policyholderOption = policyholderOption;
customerInfoClass.insureOption = insureOption;

customerInfoClass.sayHi()

// 保存，提交表单
const saveEvt = () => {
  console.log(planOption.value, '投保计划')
  console.log(policyholderOption.value, '投保人')
  console.log(insureOption.value, '被保人')
  // 调用试算接口
  apis.calPremiumEmployer({
    "salPolicy": {
      "lastyearPolicyno": "",
      "planCode": "LBZGD2024",
      "productCode": "LBZGD2024",
      "effectiveDate": "2024-11-08",
      "expireDate": "2025-11-07"
    },
    "salPolicyHolder": {
      "addfifty": "2"
    },
    "salInsuredList": [{
      "idNumber": "421023199201093835",
      "relationship": "1",
      "birthDate": "1992-01-09",
      "postCode": "plan_3",
      "epolicyPath": "",
      "linkmanname": "plan_3,0,0,0",
      "salInsuredExt": {}
    }]
  }).then((data: any) => {
    console.log('试算结果', data)
    totalPremium.value = data.totalPremium
  })
  // 校验投保计划
  const planFlag = planRef.value.validParams()
  if (!planFlag) return
  // 校验投保人
  const policyholderFlag = policyholderRef.value.validParams()
  if (!policyholderFlag) return
  // 校验被保人
  const insureFlag = insuredRef.value.validParams()
  if (!insureFlag) return


}

</script>

<style scoped>
@import url(./index.css);
</style>